import {defineStore} from "pinia";
import {store} from "@/store";
import {listPerformFreqDict} from '@/api/base/performFreqDict'
import {listSysOrgDict, listSysOrgDictShow} from '@/api/system/sysOrgDict.ts'
import {useUserStoreHook} from "@/store/modules/user";
import {listDept} from "@/api/system/dept.ts";

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
export const useDictStore = defineStore("dict", () => {
  let dictDataAllList = ref<DictData[]>([
    {
      dictLabel: "",
      dictValue: "",
      dictType: "",
    },
  ]);
  // 频次list
  const performFreqList = ref<any>([])

  // 机构用药途径字典list
  const administrationList = ref<any>([])

  // 机构科室、病区list
  const deptList = ref<any>([])

  /**
   *
   * @returns 获取机构用药途径字典
   */
  function getAdministrationList(orgId: string) {
    return new Promise((resolve, _reject) => {
      if (orgId) {
        const param = {
          orgId: orgId,
          dictType: "administration_dict"
        }
        listSysOrgDict(param).then(res => {
          administrationList.value = res.data
          resolve(res.data);
        })
      }
    })
  }

  /**
   *
   * @returns 获取机构科室、病区
   */
  function getDeptList(orgId: string
                       // , deptAttributeArray: any, clinicVsVisitArray: any,
                       // clinicalSignArray: any, internalOrSurgeryArray: any
  ) {
    const param = {
      orgId: orgId,
      // deptAttributeArray: deptAttributeArray,
      // clinicVsVisitArray: clinicVsVisitArray,
      // clinicalSignArray: clinicalSignArray,
      // internalOrSurgeryArray: internalOrSurgeryArray
    }
    listDept(param).then(res => {
      deptList.value = res.data
    })
  }

  /**
   *
   * @returns 获取全部字典
   */
  function getDataList(orgId: string) {
    return new Promise((resolve, _reject) => {
      const dictAllList: any[] = [];
      if (dictAllList && dictAllList.length > 0) {
        dictDataAllList.value = dictAllList;
        resolve(dictAllList);
      } else {
        const param = {
          orgId: orgId
        }
        listSysOrgDictShow(param).then((res: any) => {
          if (res.data) {
            dictDataAllList.value = res.data
            resolve(res.data);
          }
        })
      }
    });
  }

  /** 查询执行频次 */
  function getPerformFreqDict() {
    const param = {delFlag: '0'}
    listPerformFreqDict(param).then(res => {
      performFreqList.value = res.data
    })
  }

  /** 将获取到的字典list封装成 type label的map */
  function groupedDataMap() {
    const groups = <DictData>{};
    dictDataAllList.value.forEach((item) => {
      const dictType: string = item.dictType;
      if (!groups[dictType]) {
        groups[dictType] = [];
      }
      groups[dictType].push(item);
    });
    return groups;
    // return Object.keys(groups).map((key) => ({ key, items: groups[key] }));
  }

  /** 根据字典类型进行分类 返回Map*/
  function isTypeGetData(...arrays) {
    const groupedDataMapRet = groupedDataMap();
    const dictDataMap = <DictData>{};
    arrays.forEach((item) => {
      if (!dictDataMap[item]) {
        dictDataMap[item] = [];
      }
      dictDataMap[item] = groupedDataMapRet[item];
    });
    return dictDataMap;
  }

  function isTypeGetDataArrays(arrays: []) {
    const groupedDataMapRet = groupedDataMap();
    const dictDataMap = <DictData>{};
    arrays.forEach((item: string) => {
      if (!dictDataMap[item]) {
        dictDataMap[item] = [];
      }
      const aa = groupedDataMapRet[item];
      dictDataMap[item] = aa;
    });
    return dictDataMap;
  }

  /** 获取指定字典信息 */
  function getMateDictInfo(arrays: [], dictValue: any): any {
    const index = arrays.findIndex((dict: any) => {
      return dict.dictValue === dictValue;
    });
    if (index > -1) {
      return arrays[index];
    }
    return {};
  }

  // 通过频次id获取频次信息
  function getAppointPerformFreqInfo(dictValue: string) {
    const performFreqIndex = performFreqList.value.findIndex((performFreq: any) => {
      return dictValue === performFreq.freqId
    })
    if (performFreqIndex > -1) {
      return performFreqList.value[performFreqIndex]
    } else {
      return {}
    }
  }

  function removeDict(key) {
    try {
      for (let i = 0; i < dictDataAllList.value.length; i++) {
        if (dictDataAllList.value[i].dictType == key) {
          dictDataAllList.value.splice(i, i);
          return true;
        }
      }
    } catch (e) {
    }
  }

  return {
    dictDataAllList,
    getDataList,
    isTypeGetData,
    groupedDataMap,
    removeDict,
    isTypeGetDataArrays,
    getMateDictInfo,
    getPerformFreqDict,
    performFreqList,
    getAppointPerformFreqInfo,
    getAdministrationList,
    administrationList,
    getDeptList,
    deptList
  };
});

export function useDictStoreHook() {
  return useDictStore(store);
}
